package com.hsav.kcxb.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.hsav.kcxb.MyApplication;

import java.io.File;

/**
 * Created by zhao on 2017/2/4.
 */


public class MyUtil {

    // 参考自：http://blog.csdn.net/hitlion2008/article/details/6737459
    //解决scrollview中有listview的问题
    public static int setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return 0;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        return params.height;
    }

    public static void shardMsg(Context context,String msgTitle, String msgText, String imgPath) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        if(TextUtils.isEmpty(imgPath)){
            intent.setType("text/plain");
        }else{
            File file = new File(imgPath);
            if(file!=null && file.isFile() && file.exists()){
                intent.setType("image/*");
                Uri uri = Uri.fromFile(file);
                intent.putExtra(Intent.EXTRA_STREAM,uri);
            }
        }
        intent.putExtra(Intent.EXTRA_SUBJECT,msgTitle);
        intent.putExtra(Intent.EXTRA_TEXT,msgText);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        String activityTitle = "分享到";
        context.startActivity(Intent.createChooser(intent,activityTitle));
    }


}
